package org.sensornet2010.core;

import java.util.Date;

import org.sensornet2010.alpha.Configuration;
import org.sensornet2010.alpha.RemoteServer;
import org.sensornet2010.alpha.SensorData;
import org.sensornet2010.alpha.SoundRecorder;
import org.sensornet2010.sensmote.MoteMidlet;

public class SensornetApp extends Thread {
	
	private RemoteServer 	server;
	private SoundRecorder	soundRecorder;
	private long 			counter;
	
	private iSensornetAppListener listener;
	private String debugInfo;
	private String lastMeasurement;
	
	public void setListener(iSensornetAppListener listener){
		//TODO: create add,remove methods
		this.listener = listener;
	}
	private void notityListener(int type, String info){
		this.listener.sensorAction(type, info);
	}
	
	public SensornetApp(){
		//TODO: integrate configuration
		server = new RemoteServer(Configuration.SERVER_URL);
		soundRecorder = new SoundRecorder(Configuration.RECORDING_DURATION);
		this.debugInfo = new Date().toString() +" SensornetApp created\n";
	}

	public String getDebugInfo(){
		return this.lastMeasurement + "\n" + this.debugInfo;
	}
	
	public void run(){
		//DEBUG: System.err.println("SensorApp.run()");
		try {
			this.initialize();
			while (true){
				doMeasurement();
				this.notityListener(iSensornetAppListener.MEASUREMENT_DONE, Long.toString(this.counter));
				Thread.sleep(Configuration.RECORDING_INTERVAL*1000 - Configuration.RECORDING_DURATION);
			}
		} catch (Exception e) {
			//DEBUG: e.printStackTrace();
			this.debugInfo = new Date().toString() +"\nERROR:" + e.getClass() + "=" + e.getMessage() + "\n";
		};
	}
	
	private void initialize(){
		// testing audio capture 
		byte[] sound = soundRecorder.record();
		sound = soundRecorder.record();
		this.debugInfo += new Date().toString() +"\nAudio initialization:OK:" + sound.length + "\n";
		// testing server connection
		String status = server.getStatus();
		status = server.getStatus();
		this.debugInfo += new Date().toString() +"\nServer initialization:OK:" + status + "\n";
	}
	
	private void doMeasurement(){
		//String globalDebug =  "counter:" + counter + "\n" + (new Date()).toString();
		try {
			// audio capture
			byte[] sound = this.soundRecorder.record();
			// audio processing
			SensorData data = new SensorData(Configuration.SENSOR_ID, sound);
			data.setSensuinoVersion(Configuration.SENSUINO_VERSION);
			// send data to server
			// WARNING: if Configuration.RECORDING_INTERVAL > 15 fileUploadPost() fails
			server.getStatus();
			String info = server.sendData(data);
			//System.err.println(":" + info + ":");
			if ( ! info.startsWith("OK"))
				throw new RuntimeException("Server error" + info);
			//String info = server.getStatus();
			counter++;
			this.lastMeasurement = new Date().toString() + "\nLast measurement " + info;
			} catch (Exception e) {
				//DEBUB: e.printStackTrace();
				this.lastMeasurement = new Date().toString() +"\nERROR (measurement):" +e.getClass() + "="+ e.getMessage() + "\n";
			}
		}

		
		
}
